
<%@page import="java.awt.GraphicsConfiguration.DefaultBufferCapabilities"%>
<%@ page import="projetstackoverflow.Question" %>
<!DOCTYPE html>
<html>
	<head>
		<meta name="layout" content="main">
		<g:set var="entityName" value="${message(code: 'question.label', default: 'Question')}" />
		<title><g:message code="default.show.label" args="[entityName]" /></title>
	</head>
	<body>
	
		<div id="question-show">
			<div class="header">
				<h1>${questionInstance.title}</h1>
			</div>
			<div class="question">
				<div class="vote">
					<g:link class="up-vote" controller="message" action="upVote" params="${['id':questionInstance.id] }" />
					<p class="count-vote">${questionInstance.countVotes()}</p>
					<g:link class="down-vote" controller="message" action="downVote" params="${['id':questionInstance.id] }" />
				</div>
				<div class="post">
					<p>${questionInstance.content.replace("\r\n", "<br />")}</p>
					<g:messageInfo id="${questionInstance.id }"/>
					<g:tagList id="${questionInstance.id}"/>
					<g:if test="${questionInstance.editDate }">
						<br /><i><g:message code="question.show.lastedited"/> <g:ago time="${questionInstance.editDate }"/></i>
					</g:if>
					<div class="post-options">
						<g:if test="${session.author && (session.author.admin || session.author.id == questionInstance.author.id)}">
							<g:link controller="question" action="edit" id="${questionInstance.id }">
							<g:message code='default.question.label.edit'/>
							</g:link>
						</g:if>
						<g:if test="${session.author && session.author.admin}">
							<g:link controller="question" action="delete" id="${questionInstance.id }" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');">
							<g:message code='default.question.label.delete'/>
							</g:link>
						</g:if>
					</div>
				</div>
			</div>
			
			<g:set var="nbAnswers" value="${answersList.size()}"/>
			<div class="answers">
				<div class="header">
					<g:if test="${nbAnswers == 0}">
						<h2><g:message code="default.answers.noAnswer" /></h2>
					</g:if>
					<g:elseif test="${nbAnswers == 1}">
						<h2><g:message code="default.answers.oneAnswer" /></h2>
					</g:elseif>
					<g:else>
						<h2><g:message code="default.answers.multipleAnswers" args="${[nbAnswers]}" /></h2>
						<div id="sort-list">
							<g:link controller="question" action="show" id="${questionInstance.id }" params="[sort:'votes']" class="${(params['sort'] && params['sort']=='votes')? 'sort-choice-selected':'sort-choice'}">
								<g:message code="default.answers.bestAnswers" />
							</g:link>
							<g:link controller="question" action="show" id="${questionInstance.id }" params="[sort:'date']" class="${(!params['sort'] || (params['sort'] && params['sort']=='date'))? 'sort-choice-selected':'sort-choice'}" >
								<g:message code="default.answers.oldest" />
							</g:link>
						</div>
					</g:else>
				</div>
				<div class="list">
					<g:each in="${answersList}" var="answer">
						<div class="answer">
							<div class="vote">
								<g:link class="up-vote" controller="message" action="upVote" params="${['id':answer.id] }" />
								<p class="count-vote">${answer.countVotes()}</p>
								<g:link class="down-vote" controller="message" action="downVote" params="${['id':answer.id] }" />
								<div class="answer-checked">
									<g:if test="${answer.selected}">
										<g:if test="${session.author && (session.author.admin || session.author.id == questionInstance.author.id) }">
											<g:link controller="answer" action="unSelect" id="${answer.id }" class="answer-checked-ok" title="${message(code:'default.answer.unselect') }"/>
										</g:if>
										<g:else>
											<div class="answer-checked-ok"></div>
										</g:else>
									</g:if>
									<g:else>
										<g:if test="${session.author && (session.author.admin || session.author.id == questionInstance.author.id) }">
											<g:link controller="answer" action="select" id="${answer.id }" class="answer-checked-not" title="${message(code:'default.answer.select') }"/>
										</g:if>
									</g:else>
								</div>
							</div>
							<div class="post">
								<p>${answer.content.replace("\r\n", "<br />")}</p>
								<g:if test="${answer.editDate }">
									<br /><i><g:message code="question.show.lastedited"/> <g:ago time="${answer.editDate }"/></i>
								</g:if>
								<g:messageInfo id="${answer.id }"/>
								<div class="post-options">
									<g:if test="${session.author && (session.author.admin || session.author.id == answer.author.id)}">
										<g:link controller="answer" action="edit" id="${answer.id }">
										<g:message code='default.question.label.edit'/>
										</g:link>
									</g:if>
									<g:if test="${session.author && session.author.admin}">
										<g:link controller="answer" action="delete" id="${answer.id }" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');">
										<g:message code='default.question.label.delete'/>
										</g:link>
									</g:if>
								</div>
							</div>
						</div>
					</g:each>
				</div>
			</div>
			<g:if test="${session.author != null}">
				<div class="newAnswer">
					<div class="header">
						<g:message code="default.answers.yourAnswer"/>
					</div>
					<div class="content">
						<g:form controller="answer" action="save">
							<g:hiddenField name="question.id" value="${questionInstance.id}"/>
							<g:textArea required="" name="content" value="${answerInstance?.content}"/><br /><br />
							<g:submitButton name="create" class="save" value="${message(code: 'default.button.create.label', default: 'Create')}" />
						</g:form>
					</div>
				</div>
			</g:if>
			<g:else>
					<g:message code="default.answers.notLogged1"/><g:link controller="author" action="login" params="${['lastUri': (request.forwardURI - request.contextPath)]}"><g:message code="default.answers.notLogged2"/></g:link>
				
			</g:else>
		</div>
	</body>
</html>
